﻿Imports System.Net.NetworkInformation
Imports System.Net.Dns
Imports System.Net
Imports System.IO

Public Class Form1
    Dim i, o, u As Integer
    Dim cp As String = GetHostName()
    Dim IPv4 As String = GetHostByName(cp).AddressList(1).ToString
    Dim username, password As String
    Dim message, title, button, icons
    Dim other_links(7) As String
    Dim answer As DialogResult

    Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        
    End Sub

    Private Sub WebAdmin_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Process.Start("KF_Server_Launcher")
        pageloadTimer.start()
        u = 20
        o = 30

        If File.Exists("1.ver.verificationCertificate") = False Then

            message = "To use the following application, you need to have your local wireless adapter activated, if it is not activated exceptions may occur and the application will crash, to activate your local wireless adapter navigate to the Control panel, click on network and internet, go to the Network sharing center and at the left side of the window you have a link that spells "" Change Board Defenitions/Settings"" click it, right click the wireless adapter and click ""Activate"". Note: You need Administrative Priveleges to perform this process."
            title = "Info"
            button = MessageBoxButtons.OK
            icons = MessageBoxIcon.Information

            answer = MessageBox.Show(message, title, button, icons)

            If answer = Windows.Forms.DialogResult.OK Then
                File.Create("1.ver.verificationCertificate")
            End If
        End If




        If File.Exists("AdminDataUser.webadmin") And File.Exists("AdminDataPass.webadmin") Then
            Dim userReader As New StreamReader("AdminDataUser.webadmin")
            Dim passReader As New StreamReader("AdminDataPass.webadmin")
            username = userReader.ReadToEnd()
            userReader.Close()
            password = passReader.ReadToEnd()
            passReader.Close()

        ElseIf File.Exists("AdminDataUser.webadmin") And File.Exists("AdminDataPass.webadmin") = False Then
            Dim userReader As New StreamReader("AdminDataUser.webadmin")
            Dim passReader As New StreamReader("AdminDataPass.webadmin")
            username = userReader.ReadToEnd()
            userReader.Close()
            password = InputBox("Please Insert your Server Admin Password: ")
        ElseIf File.Exists("AdminDataUser.webadmin") = False And File.Exists("AdminDataPass.webadmin") Then
            Dim userReader As New StreamReader("AdminDataUser.webadmin")
            Dim passReader As New StreamReader("AdminDataPass.webadmin")
            username = InputBox("Plese Insert your Server Admin Name: ")
            password = passReader.ReadToEnd
            passReader.Close()

        Else

            username = InputBox("Plese Insert your Server Admin Name: ")
            password = InputBox("Please Insert your Server Admin Password: ")

            message = "Do you wish to save your data in this computer?"
            title = "Save?"
            button = MessageBoxButtons.YesNo
            icons = MessageBoxIcon.Question

            answer = MessageBox.Show(message, title, button, icons)

            If answer = Windows.Forms.DialogResult.Yes Then
                Dim userWriter As New StreamWriter("AdminDataUser.webadmin")
                userWriter.Write(username)
                userWriter.Close()
                Dim passWriter As New StreamWriter("AdminDataPass.webadmin")
                passWriter.Write(password)
                passWriter.Close()
            End If

        End If
        Try

            other_links(0) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/currentframe?Page=current_game"
            other_links(1) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdsKF/"
            other_links(2) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/currentframe?Page=current_players"
            other_links(3) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/currentframe?Page=current_console"
            other_links(4) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/currentframe?Page=current_mutators"
            other_links(5) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/currentframe?Page=current_restart"
            other_links(6) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/defaultsframe?GameType=KFMod.KFGameType&Page=defaults_maps"
            other_links(7) = "http://" & username & ":" & password & "@" & IPv4 & ":8075/ServerAdmin/admins_home"


        Catch ex As NullReferenceException
        End Try

    End Sub

    Private Sub Refresh_Click(sender As Object, e As EventArgs) Handles Refresh1.Click
        If WebAdminTabs.SelectedIndex = 0 Then
            WebAdminBrowser.Refresh()
        Else
            ServerAdsBrowser.Refresh()
        End If
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        WebBrowser1.Navigate(other_links(5))
    End Sub

    Private Sub ReloadTimer_Tick(sender As Object, e As EventArgs) Handles ReloadTimer.Tick
        If o >= 0 Then
            Label1.Text = "Reloading in: " & o & " s"
        End If
        i += 1
        o -= 1


        If i = 30 Then
            WebAdminBrowser.AllowNavigation = True
            WebAdminBrowser.Refresh()
            PlayerList.AllowNavigation = True
            PlayerList.Refresh()
            ConsoleAdm.AllowNavigation = True
            ConsoleAdm.Refresh()
            TimerTimer.Start()
            o = 30
            i = 0
        End If
    End Sub

    Private Sub TimerTimer_Tick(sender As Object, e As EventArgs) Handles TimerTimer.Tick
        u -= 1
        If u = 0 Then
            ConsoleAdm.AllowNavigation = False
            WebAdminBrowser.AllowNavigation = False
            PlayerList.AllowNavigation = False
            u = 5
            TimerTimer.Stop()
        End If
    End Sub

    Private Sub pageloadTimer_Tick(sender As Object, e As EventArgs) Handles pageloadTimer.Tick
        Me.Text = "Loading in: " & u
        u -= 1
        If u = 0 Then
            WebAdminBrowser.Navigate(other_links(0))
            ServerAdsBrowser.Navigate(other_links(1))
            PlayerList.Navigate(other_links(2))
            ConsoleAdm.Navigate(other_links(3))
            Mutators.Navigate(other_links(4))
            Def.Navigate(other_links(6))
            AdmGroupie.Navigate(other_links(7))
            TimerTimer.Start()
            ReloadTimer.Start()
            pageloadTimer.Stop()
            Me.Text = "Web Admin"
        End If
    End Sub
End Class
